<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>公交线路查询</title>
    <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
    <script type="text/javascript"
            src="http://webapi.amap.com/maps?v=1.3&key=4b62747cb9fa527944ec3f3dd29f6c07&plugin=AMap.LineSearch"></script>
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id="container"></div>
<script language="javascript">
    /*
     * 该示例主要流程分为三个步骤
     * 1. 首先调用公交路线查询服务(lineSearch)
     * 2. 根据返回结果解析，输出解析结果(lineSearch_Callback)
     * 3. 在地图上绘制公交线路()
     */
    var zoom = 13;
    var map = new AMap.Map("container", {
        resizeEnable: true,
        center: [111.674231, 40.827063],//地图中心点
        zoom: zoom //地图显示的缩放级别
    });

    //    var satellLayer = new AMap.TileLayer.Satellite({zIndex:10}); //实例化卫星图
    //    satellLayer.setMap(map); //在map中添加卫星图
    map.setMapStyle('amap://styles/82a9110e340da9661c851e31d6f42698');
    //加载鹰眼
    var overview = map.plugin(
            ["AMap.OverView"],
            function () {
                view = new AMap.OverView();
                map.addControl(view);
            }
    );
    //    overview.hide();
    var scale = map.plugin(
            ["AMap.Scale"],
            function () {
                var scale = new AMap.Scale();
                map.addControl(scale);
            }
    );
    //    scale.hide();

    var toolbar = map.plugin(["AMap.ToolBar"],
            function () { //加载工具条
                var tool = new AMap.ToolBar();
                map.addControl(tool);
            }
    );


    //    toolbar.hide();
    /*公交线路查询*/
    function lineSearch() {
        //实例化公交线路查询类，只取回一条路线
        var linesearch = new AMap.LineSearch({
            pageIndex: 1,
            city: '呼和浩特',
            pageSize: 1,
            extensions: 'all'
        });
        var url = "{:url('getBusLineName')}";
        $.ajax({
            url: url,
            dataType: 'json',
            success: function (data, status) {
                for (var i = 0; i < data.length; i++) {
                    //搜索“536”相关公交线路
                    linesearch.search(data[i], function (status, result) {
                        if (status === 'complete' && result.info === 'OK') {
                            lineSearch_Callback(result,data[i]);
                        } else {
                            console.log(data[i] + "线路未找到");
                            console.log(result);
                        }
                    });
                }
            }
        });
    }

    /*公交路线查询服务返回数据解析概况*/
    function lineSearch_Callback(data) {
        var lineArr = data.lineInfo;
        var lineNum = data.lineInfo.length;
        if (lineNum == 0) {
            console.log(name + "线路未找到");
        } else {
            for (var i = 0; i < lineNum; i++) {
                var id = lineArr[i].id;
                var total_name = lineArr[i].name;
                var company = lineArr[i].company;
                var pathArr = lineArr[i].path;
                var stops = lineArr[i].via_stops;
                var startPot = stops[0].location;
                var endPot = stops[stops.length - 1].location;
                data={
                    id:id,
                    total_name:total_name,
                    company:company,
                    path:JSON.stringify(lineArr[i].path),
                    stations:JSON.stringify(stops),
                };
                var url = "{:url('uploadLine')}";
                $.ajax({
                    url: url,
                    dataType: 'json',
                    data:data,
                    type:'post',
                    success: function (data, status) {

                    }
                });


                if (i == 0) drawbusLine(startPot, endPot, pathArr);
            }
        }
    }

    /*绘制路线*/
    function drawbusLine(startPot, endPot, BusArr) {
        //绘制起点，终点
//        new AMap.Marker({
//            map: map,
//            position: [startPot.lng, startPot.lat], //基点位置
//            icon: "http://webapi.amap.com/theme/v1.3/markers/n/start.png",
//            zIndex: 10
//        });
//        new AMap.Marker({
//            map: map,
//            position: [endPot.lng, endPot.lat], //基点位置
//            icon: "http://webapi.amap.com/theme/v1.3/markers/n/end.png",
//            zIndex: 10
//        });

        var color = getColor();
        //绘制乘车的路线
        busPolyline = new AMap.Polyline({
            map: map,
            path: BusArr,
            strokeColor: color,//线颜色
            strokeOpacity: 0.8,//线透明度
            strokeWeight: 3,//线宽
            strokeStyle:'dashed',
            geodesic: true,
            strokeDasharray: [10, 30],
//            showDir: true,
        });
        map.setFitView();
    }

    r = 0x0;
    g = 0x0;
    b = 0x0;
    index = 0;
    var colors = ['red', 'green', 'blue', 'gray', 'yellow', 'purple'];
    //    x=0x0;
    function getColor() {
//        r+=0x3;
//        g+=0x3;
//        b+=0x3;

        return colors[index++ % colors.length];
    }
    ;
    lineSearch();

</script>
</body>
</html>